$().ready(function(){
    $('#signupform').submit(function(){
        $('input').each ( function() {
            $(this).focus();
        });

        if ( verifyusername ($("#username").val()) != 1 )
            return false;

        if ( verifyemailaddress ($("#email").val()) != 1 )
            return false;

        if ( verifypassword ($("#password").val()) != 1 )
            return false;
 
        return true;        
    });

    $('.valid').focusout ( 
        function() {

        switch ($(this).attr("name")) {
            case 'username':
                var result = verifyusername ($(this).val());
                $(this).parent().parent().children().eq(2).remove();
                if ( result != 1 )
                    $(this).parent().parent().append (result);
                else
                    $(this).parent().parent().children().eq(2).remove();
            break;
            case 'email':
                var result = verifyemailaddress ($(this).val());
                $(this).parent().parent().children().eq(2).remove();
                if ( result != 1 )
                    $(this).parent().parent().append(result);
            break;
            case 'password': case 'confirmpassword':
                var result = verifypassword ($("#password").val());
                $("#password").parent().parent().children().eq(2).remove();
                if ( result != 1 )
                    $("#password").parent().parent().append(result);
            break;
        }

    });
    function verifypassword (pass) {
        if ( pass == null || pass.length < 1 || pass.length > 255 )
            return "<td class=\"signup\">Password is required </td>";

        var passReg = /^([\S]{8,255})?$/;
        if ( !passReg.test(pass))
            return "<td class=\"signup\">Enter a valid password (no spaces). Must be greater than 8 characters in length</td>";
        
        var confPass = $("#confirmpassword").val();
        if ( pass != confPass ) 
            return "<td class=\"signup\">Passwords don't match</td>";
        else
            return 1;
    }
    function verifyemailaddress(emailaddressVal) {
        if ( emailaddressVal == null || emailaddressVal.length < 1 || emailaddressVal.length > 255)
            return "<td class=\"signup\">Email address is required </td>";

        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        if ( !emailReg.test(emailaddressVal)) 
            return "<td class=\"signup\">Enter a valid email address </td>";
        else
            return 1;
        
    }
    function verifyusername (username) {
        if (username == null || username.length < 1 || username.length > 255)
            return "<td class=\"signup\">User name is required </td>";
            
        var userReg = /^([\w-\.]{4,255})?$/;
        if ( !userReg.test(username) )
            return "<td class=\"signup\">Enter a valid user name. Atleast of length 4 and no spaces</td>";
        else
            return 1;
    }
});